package com.kgc.server.service.Impl;

import com.kgc.server.dao.AddressDao;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("interfaceSecurityService")
public class InterfaceSecurityServiceImpl {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private AddressDao addressDao;

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    public String setRedisNum(){
        logger.info("InterfaceSecurityServiceImpl setRedisNum start...");
        List numList = new ArrayList();
        redisTemplate.delete("numList");
        for(int i=0;i<100;i++){
            numList.add((int)Math.floor(Math.random() * (1000 - 1 + 1))+1);
        }
        logger.info("InterfaceSecurityServiceImpl setRedisNum numList:"+numList);
        redisTemplate.opsForValue().set("numList", numList);
        int num = (int)Math.floor(Math.random() * (10 - 0 + 1))+1;
        logger.info("InterfaceSecurityServiceImpl setRedisNum numList:"+numList+",num:"+num);
        List list = (List)redisTemplate.opsForValue().get("numList");
        logger.info("InterfaceSecurityServiceImpl setRedisNum numList:"+numList+",num:"+num+",list:"+list);
        return list.get(num)+"";
    }

    public String returnInterfaceStr(Map map,long TimeStamp,String randomNum,String encryptionStr){
        logger.info("InterfaceSecurityServiceImpl returnInterfaceStr map:"+map+",TimeStamp:"+TimeStamp+",randomNum:"+randomNum+",encryptionStr:"+encryptionStr);
        //判断时间戳 是否是非法请求
        Date date = new Date();
        long newTimeStamp = date.getTime();
        System.out.println("后端时间戳:"+newTimeStamp);
        System.out.println("前端时间戳:"+TimeStamp);

        if( newTimeStamp-TimeStamp > 10000 ){
            return "超时请求!";
        }
        logger.info("InterfaceSecurityServiceImpl returnInterfaceStr map:"+map+",TimeStamp:"+TimeStamp+",randomNum:"+randomNum+",encryptionStr:"+encryptionStr+",newTimeStamp:"+newTimeStamp);

        String jsonStr = "";
        for(Object MapKey : map.entrySet()){
            jsonStr += MapKey + "&";
        }
        logger.info("InterfaceSecurityServiceImpl returnInterfaceStr map:"+map+",TimeStamp:"+TimeStamp+",randomNum:"+randomNum+",encryptionStr:"+encryptionStr+",newTimeStamp:"+newTimeStamp+",jsonStr:"+jsonStr);
        jsonStr = jsonStr.substring(0,jsonStr.length()-1);

        String str = DigestUtils.md5Hex(jsonStr+randomNum);
        logger.info("InterfaceSecurityServiceImpl returnInterfaceStr map:"+map+",TimeStamp:"+TimeStamp+",randomNum:"+randomNum+",encryptionStr:"+encryptionStr+",newTimeStamp:"+newTimeStamp+",jsonStr:"+jsonStr+",str:"+str);

        if( str.equals(encryptionStr) ){
            List list = (List)redisTemplate.opsForValue().get("numList");
            for(int i=0;i<list.size();i++){
                if( list.get(i).equals(randomNum) ){
                    redisTemplate.delete(list.get(i).equals(randomNum));
                }
            }
            return "成功请求!";
        }
        return "失败请求!";
    }
}
